const { storeBindingsBehavior } = require("mobx-miniprogram-bindings")
const store = require("../../store/index")

Component({
  behaviors: [storeBindingsBehavior],
  options: {
    pureDataPattern: /^_/ // 指定所有 _ 开头的数据字段为纯数据字段
  },

  data: {
    latitude: 0,
    longitude: 0,
    markers: [],
    _mapctx: null
  },

  storeBindings: {
    store,
    // 将store的状态数据映射到当前组件上
    fields: {
      markers: 'markers'
    },
    // 将store的操作状态的方法映射到当前组件上
    actions: {
      loadData: "loadData",
    },
  },

  lifetimes: {
    ready() {
      // 获取地图对象
      this.data._mapctx = wx.createMapContext("map")
      
      // 获取用户位置
      wx.getLocation({
        type: 'wgs84',
        success: (res) => {
          const latitude = res.latitude
          const longitude = res.longitude
          
          this.setData({
            latitude,
            longitude
          })
        }
      })
    }
  },

  pageLifetimes: {
    show() {
      this._loadData()
    }
  },

  methods: {
    handlePublish() {
      wx.navigateTo({
        url: '/pages/publish/publish',
      })
    },

    _loadData() {
      this.loadData()
    },

    handleMarkerTap(e) {
      wx.navigateTo({
        url: '/pages/detail/detail?id=' + e.detail.markerId,
      })
    }
  }
})